﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Util;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.CO;
using HiStaff.Util.Enum;
using HiStaff.iSystem;
namespace HiStaff.iTime
{
    public partial class OtherSignRegister : BaseRegister
    {       
        public OtherSignRegister()
        {
            InitializeComponent();
        }

        protected override bool FormLoad()
        {
            base.CreateCommandBar(CommandBarManager.ActionCommand.New,
                                      CommandBarManager.ActionCommand.Separator,
                                      CommandBarManager.ActionCommand.Save,
                                      CommandBarManager.ActionCommand.Cancel,
                                      CommandBarManager.ActionCommand.Separator,
                                      CommandBarManager.ActionCommand.Exit);
            empSelect.EmployeeSelected = ListEmployeeRegist;

            cboSign.DisplayMember = "DISPLAYNAME";
            cboSign.ValueMember = "CODE";

            var lstSign = HiStaff.iSystem.Data.ALLCOSIGN.FindAll(obj => obj.STATUS == 1 &&
                obj.GROUPID == (decimal?)WorkSignGroup.OtherSign &&
                obj.DATAMODEID == (decimal?)WorkSignMode.Regist);
            cboSign.SetDataBinding(lstSign, "CODE");
            txtFromDate.Value = DateTime.Today;
            txtToDate.Value = DateTime.Now;

            return base.FormLoad();
        }
        protected override bool CheckValidation(System.Windows.Forms.Control container)
        {
            if (!base.CheckValidation(container))
                return false;
            else
            {
                if (txtFromDate.Value > txtToDate.Value)
                {
                    MessageBox.Show(GetMessageById(Constant.MSG_INVALID_DATE), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
                CO_SUM_SEARCH objSearch;
                foreach (var item in empSelect.EmployeeSelected)
                {
                    objSearch = new CO_SUM_SEARCH();
                    objSearch.EMPLOYEEID = item.ID;
                    objSearch.STARTDATE = txtFromDate.Value;
                    objSearch.ENDDATE = txtToDate.Value;
                    Service.ExcuteStoreNonQuery("PKG_CALCOLEX", "PRS_CHKCOSTATUS", objSearch);

                    if (objSearch.CLOSEPERIOD > 0)
                    {
                        MessageBox.Show(GetMessageById(Constant.MSG_INVALID_PERIOD), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return false;
                    }
                }
                return true;
            }
        }
        protected override bool SaveButtonClick()
        {
            CO_SIGN_LST objSign;
            CO_DAYRGT objUpdate;
            DateTime workingdate;

            foreach (var item in empSelect.EmployeeSelected)
            {
                decimal outvalue;
                DateTime dateout;
                workingdate = txtFromDate.Value.Value;
                while (workingdate <= txtToDate.Value.Value)
                {
                    objSign = (CO_SIGN_LST)cboSign.SelectedItem;
                    objUpdate = new CO_DAYRGT();
                    objUpdate.EMPLOYEEID = item.ID;
                    objUpdate.CODE = objSign.CODE;
                    objUpdate.WORKINGDAY = workingdate;
                    objUpdate.NOTE = txtNote.Text.Trim();

                    switch ((WorkSignDataType)objSign.DATATYPEID)
                    {
                        case WorkSignDataType.Number:
                            if (decimal.TryParse(txtValue.Text, out outvalue))
                            {
                                objUpdate.NVALUE = outvalue;
                            }
                            else
                            {
                                MessageBox.Show(GetMessageById(Constant.MSG_INVALID_VALUE), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                txtValue.Focus();
                                return false;
                            }
                            break;

                        case WorkSignDataType.String:
                            objUpdate.SVALUE = txtValue.Text;
                            break;

                        case WorkSignDataType.DateTime:
                            if (DateTime.TryParse(txtValue.Text, out dateout) ||
                                DateTime.TryParseExact(txtValue.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out dateout) ||
                                 DateTime.TryParseExact(txtValue.Text, "dd/MM/yyyy HH:mm:ss", null, System.Globalization.DateTimeStyles.None, out dateout))
                            {
                                objUpdate.DVALUE = dateout;
                            }
                            else
                            {
                                MessageBox.Show(GetMessageById(Constant.MSG_INVALID_VALUE), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                txtValue.Focus();
                                return false;
                            }
                            break;

                        default:
                            break;
                    }

                    Service.ExcuteStoreNonQuery("PRI_CO_DAYRGT", objUpdate);
                    if (!string.IsNullOrEmpty(objUpdate.ERRCODE))
                    {
                        MessageBox.Show(objUpdate.ERRMSG, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return false;
                    }

                    workingdate = workingdate.AddDays(1);
                }
            }

            MessageBox.Show(GetMessageById(Constant.MSG_RGT_SUCCESS), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

            ResetInputControl(this);

            this.RegisterResult = DialogResult.OK;

            return true;
        }
    }
}
